﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script>
        $(function () {
        //获取角色信息
        $.ajax({
            type: "get",
            url: "/Quanxian/role",
            success: function (data) {
                $(data).each(function (index, item) {
                    var str = '<option value="' + item.Rid + '">' + item.Rname + '</option>';
                    $("#select1").append(str);
                })
            }
        })
        //权限信息
        //一级菜单权限
        $.ajax({

            type: "get",
            url: "/Quanxian/Option",
            asyn: false,//同步
            data: {
                pid: 0
            },
            success: function (data) {
                
                $(data).each(function (index, item) {
                    var str = '<span id="S' + item.Oid + '"><input type="checkbox" value="' + item.Oid + '" id="ss' + item.Oid + '" onclick="fun(' + item.Oid + ')"/>' + item.Oname + '<br/></span>';
                    $("#div1").append(str);
                    var pid = item.Oid;
                    //根据父级id获取下级权限信息（二级菜单）
                    $.ajax({
                        type: "get",
                        url: "/Quanxian/Option",
                        asyn: false,//同步
                        data: {
                            pid: pid
                        },
                        success: function (data) {
                            $(data).each(function (index, item) {
                                var str1 = '<input type="checkbox" value="' + item.Oid + '" name="S' + pid + '"/>' + item.Oname + '';
                                $("#S" + pid + "").append(str1);

                            })
                            var ss = "<br/>";
                            $("#S" + pid + "").append(ss);
                        }
                    })
                })
            }
        })
        $("#select1").change(function () {

            $("input:checkbox").attr("checked", false);
            //显示当前角色下的权限
            $.ajax({
                type: "get",
                url: "/Quanxian/getOidbyrid",
                cache: false,//清除缓存
                data: {
                    pid: $("#select1").val()
                },
                success: function (data) {
                    $(data).each(function (index, item) {
                        $("input:checkbox").each(function () {
                            if ($(this).val() == item.Oid) {
                                $(this).attr("checked", true);
                            }
                        })
                    })
                }
            })
        })

    })
    //全选
    function fun(id) {
        var str = $("#ss" + id + "").attr("checked");
        if (str == "checked") {
            $(":checkbox[name='S" + id + "']").attr("checked", true);
        }
        else {
            $(":checkbox[name='S" + id + "']").removeAttr("checked");
        }
    }
    //修改权限
    function update() {
        var time = (new Date()).valueOf();
        var str = "";
        $(":checkbox:checked").each(function () {
            str += $(this).val() + ",";
        })
        str = str.substr(0, str.length - 1);

        $.ajax({
            type: "post",
            url: "/Quanxian/update",
            data: {
                pid: $("#select1").val(),
                str: str
            },
            success: function (data) {
                if (data > 0) {
                    alert("修改成功");
                }
                else {
                    alert("修改失败");
                }
            }
        })
    }

    </script>

</head>
<body>
    <div>
        <select id="select1">
            <option value="">-请选择-</option>
        </select>
        <div id="div1"></div>
        <input type="submit" value="修改" onclick="update()" />
    </div>
</body>
</html>
